package com.edu.flying_birds;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @PackageName: com.edu.flying_birds
 * @ClassName: Column
 * @Description: 柱子类
 * @author: RZS
 * @date: 2021/6/18  15:01
 */
public class Column {
    // 定义柱子的x, y坐标
    public int x;
    public int y;
    // 定义柱子的宽度和高度
    public int width;
    public int height;
    // 定义存放柱子图片的变量
    public BufferedImage img;
    // 添加两根柱子的距离变量
    public int distance = 240;
    // 定义随机数
    public Random random = new Random();

    public Column(int number) {
        try {
            init(number);
        } catch (IOException e) {
            System.out.println("初始化柱子失败");
            e.printStackTrace();
        }
    }

    /**
     * 初始化方法
     */
    private void init(int num) throws IOException {
        img = ImgUtils.getImg("../img/column.png");
        width = img.getWidth()/2;
        height = img.getHeight()/2;
        x = 320 + distance*(num == 1 ? 0:1);
        y = -(random.nextInt(height-(644-146) + 1));
    }

    /**
     * 柱子移动
     */
    public void move(){
        // 判断柱子是否移动出窗口
        if(x < -width){
            x = 320 + distance;
            y = -(random.nextInt(height-(644-146) + 1));
        }
       x--;
    }
}
